<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Member_handle extends MY_Controller
{
    function get_list()
    {
        $begin = ( int ) $this->input->get( 'begin' );
        $length = ( int ) $this->input->get( 'length' );
        
        $begin = $begin < 0 ? 0 : $begin;
        $length = $length < 1 ? 10 : $length;
        
        $this->load->model( 'member_model' );
        $count = $this->member_model->get_list_count();
        if ($begin > 0 && $begin >= $count)
        {
            $this->rs->err( 4 );
        }
        $list = $this->member_model->get_list( $length, $begin );
        $this->rs->ok( array (
                'record_count' => $count,
                'list' => $list 
        ) );
    }
    function get()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'member_model' );
        if ($content = $this->member_model->get( $id ))
        {
            $this->rs->ok( $content );
        }
        $this->rs->err( '内容不存在！' );
    }
    function modify()
    {
        //TODO:关键信息修改后，需清除用户会话状态         
        $this->load->model( 'member_model' );

        // 加载会员辅助
        if (! is_file( $file = APPPATH . 'include/MemberHelper.class.php' ))
        {
            show_error( 'The class file MemberHelper.class.php does not exist.' );
        }
        include ($file);
        
        $id = ( int ) $this->input->post( 'id' );
        $is_insert = $id > 0 ? false : true;
        $modify_data = array ();
        
        $deleted = $this->input->post( 'deleted' );
        if ($is_insert || isset( $deleted ))
        {
            $modify_data['deleted'] = $deleted ? 1 : 0;
        }
        
        $username = $this->input->post( 'username' );
        if ($is_insert || isset( $username ))
        {
            if (empty( $username ))
            {
                $this->rs->err( '用户名不能为空' );
            }
            if (preg_match( '#^\d.*|.*@.*$#', $username ))
            {
                $this->rs->err( '用户名不能以数字开头或者包含@符号' );
            }
            $check_id = MemberHelper::check_member( $username );
            if ($check_id && $check_id != $id)
            {
                $this->rs->err( '用户名已存在！' );
            }
            $modify_data['username'] = $username;
        }
        
        $nickname = $this->input->post( 'nickname' );
        if ($is_insert || isset( $nickname ))
        {
            $modify_data['nickname'] = substr_utf8( $nickname, 32, '' );
        }
        
        $picture = $this->input->post( 'picture' );
        if ($is_insert || isset( $picture ))
        {
            $modify_data['picture'] = substr_utf8( $picture, 255, '' );
        }
        
        $role_id = $this->input->post( 'role_id' );
        if ($is_insert || isset( $role_id ))
        {
            $modify_data['role_id'] = ( int ) $role_id;
        }
        
        $enable = $this->input->post( 'enable' );
        if ($is_insert || isset( $enable ))
        {
            $modify_data['enable'] = $enable ? 1 : 0;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        if ($is_insert)
        {            
            $password = $this->input->post( 'password' );
            if (empty( $password ))
            {
                $this->rs->err( '密码不能为空' );
            }
            $modify_data['password'] = MemberHelper::encrypt_password( $password );
            
            $modify_data['add_time'] = $_SERVER['REQUEST_TIME'];
            
            if ($uid = $this->member_model->insert( $modify_data ))
            {
                $this->rs->ok( array (
                        'id' => $uid,
                        'username' => $username 
                ) );
            }
        }
        else
        {
            $password = $this->input->post( 'password' );
            if (! empty( $password ))
            {
                $modify_data['password'] = MemberHelper::encrypt_password( $password );
            }
            
            if (false !== $this->member_model->update( $modify_data, $id ))
            {
                $this->rs->ok();
            }
        }
        $this->rs->err();
    }
    function clean()
    {
        // 加载会员辅助
        if (! is_file( $file = APPPATH . 'include/MemberHelper.class.php' ))
        {
            show_error( 'The class file MemberHelper.class.php does not exist.' );
        }
        include ($file);
        
        $id = ( int ) $this->input->post( 'id' );
        if (MemberHelper::clean( $id ))
        {
            $this->rs->ok();
        }
        $this->rs->err();
    }
    function password()
    {
        // 加载会员辅助
        if (! is_file( $file = APPPATH . 'include/MemberHelper.class.php' ))
        {
            show_error( 'The class file MemberHelper.class.php does not exist.' );
        }
        include ($file);
        
        // $this->rs->ok( MemberHelper::encrypt_password( $this->input->get( 'p' ) ) );
    }
}